﻿using System;

namespace Program19
{
    class Program
    {
        public static void Main(string[] args)
        {
            //1.用方法来实现：有一个字符串数组：{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" }，请输出最长的字符串
            string[] str = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            string longesStr = GetLongestStr(str);
            Console.WriteLine(longesStr);
            Console.ReadKey();

            //2.用方法来实现：请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }。要求：计算结果如果有小数，则显示小数点后两位（四舍五入）。
            int[] a = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 };
            double aer = GetAverageValue(a);
            Console.WriteLine("平均值是{0:f}", aer);
            Console.ReadKey();

            //3.请通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。
            int[] nums = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            Array.Sort(nums);

            for(var i = 0; i < nums.Length; i++)
            {
                Console.Write(nums[i] + " ");
            }

            //冒泡排序
            for(var i = 0; i < nums.Length; i++)
            {
                for(var j = 0; j < nums.Length; j++)
                {
                    if (nums[i] > nums[i])
                    {
                        //如果当前元素大于后面元素，他们进行交换
                        var tmp = nums[i];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = tmp;
                    }
                }
            }

        }
        /// <summary>
        /// 取最长的字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static string GetLongestStr(string[] str)
        {
            string longStr = str[0];
            for (int i = 1; i < str.Length; i++)
            {
                if (str[i].Length > longStr.Length)
                {
                    longStr = str[i];
                }
            }
            return longStr;
        }
        public static double GetAverageValue(int[] numArray)
        {
            int sum = 0;
            for(int i = 0; i < numArray.Length; i++)
            {
                sum += numArray[i];
            }
            //Math.Round()对数字进行四舍五入
            return Math.Round(sum * 1.0 / numArray.Length, 2);
        }
    }
}
